import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pistola here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pistola extends Arma
{
    private int dir;
    private GreenfootImage images[];
    private GreenfootSound pis=new GreenfootSound("pistola.mp3");
    private int balas;
    int letcont=0;
    Letrero le,im;
    String cadena;
    /**
     * Act - do whatever the Ametralladora wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Pistola(){
      images=new GreenfootImage[4];
      images[0]=new GreenfootImage("pistola.png");
      images[1]=new GreenfootImage("pistola.png");
      images[1].mirrorHorizontally();
       balas=9;
       cadena="";
       cadena = String.valueOf(balas);
       cadena= Integer.toString(balas);
       le=new Letrero(cadena,0);
       im=new Letrero(images[0],0);
       
    }
    public void act() 
    { 
        Mundo aux;
        aux=(Mundo)getWorld();
        Cartman c;
        c=aux.getCartman();
        dir=c.getDir();
        if(letcont==0){
        getWorld().addObject(le,360,20);
        getWorld().addObject(im,300,20);
        
        }
        cadena = String.valueOf(balas);
        cadena= Integer.toString(balas);
        le.updateImage(cadena);
        if(dir==1)
         setImage(images[0]);
        
        if(dir==2)
         setImage(images[1]);
        
        
        setLocation(c.getX(),c.getY()+14);
        if(c.getDisparo()==1){
           disparo();
           c.setDisparo();
        }
        
    }    
    
    public void disparo(){
        
       if(balas>0){
         pis.play();
         Bala b=new Bala(15,5,dir);
         World w=getWorld();
         w.addObject(b,getX(),getY());
   
         Greenfoot.delay(10);
         balas--;
       }
       
    
    }
    public int getBalas(){
      return balas;
    }
    public void setBalas(int num){
       balas+=num;
    }
    public void borrar()
    {
        getWorld().removeObject(le);
        getWorld().removeObject(im);
        getWorld().removeObject(this);
    }
}
